package com.tongshu.naive.chat.socket.handler;

import com.alibaba.fastjson.JSON;
import com.tongshu.naive.chat.application.UIService;
import com.tongshu.naive.chat.ui.view.chat.IChatMethod;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import javafx.application.Platform;
import protocol.login.LoginResponse;
import protocol.login.dto.ChatRecordDto;
import protocol.login.dto.ChatTalkDto;
import protocol.login.dto.GroupsDto;
import protocol.login.dto.UserFriendDto;

import java.util.List;

public class LoginHandler extends SimpleChannelInboundHandler<LoginResponse> {
    private UIService uiService;

    public LoginHandler(UIService uiService) {
        this.uiService = uiService;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, LoginResponse loginResponse) throws Exception {
        System.out.println("\r\n> msg handler ing ...");
        System.out.println("消息内容：" + JSON.toJSONString(loginResponse));
        if (!loginResponse.isSuccess()) {
            System.out.println("登陆失败");
            return;
        }

        Platform.runLater(() -> {
            uiService.getLoginMethod().doLoginSuccess();
            IChatMethod chatMethod = uiService.getiChatMethod();
            // 设置用户信息
            chatMethod.setUserInfo(loginResponse.getUserId(), loginResponse.getUserNickName(), loginResponse.getUserHead());
            // 对话框
            List<ChatTalkDto> chatTalkList = loginResponse.getChatTalkList();
            if (null != chatTalkList) {
                chatTalkList.forEach(talk -> {
                    chatMethod.addTalkBox(0, talk.getTalkType(), talk.getTalkId(), talk.getTalkName(), talk.getTalkHead(), talk.getTalkSketch(), talk.getTalkDate(), true);
                    switch (talk.getTalkType()) {
                        // 好友
                        case 0:
                            List<ChatRecordDto> userRecordList = talk.getChatRecordList();
                            if (null == userRecordList || userRecordList.isEmpty()) return;
                            for (int i = userRecordList.size() - 1; i >= 0; i--) {
                                ChatRecordDto chatRecordDto = userRecordList.get(i);
                                // 自己的消息
                                if (0 == chatRecordDto.getMsgType()) {
                                    chatMethod.addTalkMsgRight(chatRecordDto.getTalkId(), chatRecordDto.getMsgContent(), chatRecordDto.getMsgType(), chatRecordDto.getMsgDate(), true, false, false);
                                    continue;
                                }
                                // 好友的消息
                                if (1 == chatRecordDto.getMsgType()) {
                                    chatMethod.addTalkMsgUserLeft(chatRecordDto.getTalkId(), chatRecordDto.getMsgContent(), chatRecordDto.getMsgType(), chatRecordDto.getMsgDate(), true, false, false);
                                }
                            }
                            break;
                        // 群组
                        case 1:
                            List<ChatRecordDto> groupRecordList = talk.getChatRecordList();
                            if (null == groupRecordList || groupRecordList.isEmpty()) return;
                            for (int i = groupRecordList.size() - 1; i >= 0; i--) {
                                ChatRecordDto chatRecord = groupRecordList.get(i);
                                //  自己的消息
                                if (0 == chatRecord.getMsgUserType()) {
                                    chatMethod.addTalkMsgRight(chatRecord.getTalkId(), chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                                    continue;
                                }
                                // 他人的消息
                                if (1 == chatRecord.getMsgUserType()) {
                                    chatMethod.addTalkMsgGroupLeft(chatRecord.getTalkId(), chatRecord.getUserId(), chatRecord.getUserNickName(), chatRecord.getUserHead(), chatRecord.getMsgContent(), chatRecord.getMsgType(), chatRecord.getMsgDate(), true, false, false);
                                }
                            }
                            break;
                        default:
                            break;
                    }
                });
            }
            // 群组
            List<GroupsDto> groupsDtoList = loginResponse.getGroupsList();
            if (null != groupsDtoList) {
                groupsDtoList.forEach(groupsDto -> chatMethod.addFriendGroup(groupsDto.getGroupId(), groupsDto.getGroupName(),groupsDto.getGroupHead()));
            }
            // 好友
            List<UserFriendDto> userFriendDtoList = loginResponse.getUserFriendList();
            if (null != userFriendDtoList) {
                userFriendDtoList.forEach(userFriendDto -> chatMethod.addFriendUser(false, userFriendDto.getFriendId(), userFriendDto.getFriendName(), userFriendDto.getFriendHead()));
            }
        });
    }

}
